package it.gat.mobile;

import it.gat.controller.utils.GatSession;
import it.gat.model.Cliente;
import it.gat.model.Operatore;
import it.gat.model.RapportoIntervento;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		
		private TextView textUtente;
		private Button btnNuovoTicket;
		private Button btnContratto;
		private Button btnTicket;
		private Button btnRapporto;
		private Button btnNuovoRapporto;
		private Button btnCalendario;

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			final View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			
			textUtente = (TextView) rootView.findViewById(R.id.textUtente);
			textUtente.setText("Utente:"+GatSession.getInstance().getUtenteLoggato().getCognome() + " " + GatSession.getInstance().getUtenteLoggato().getNome());
			
			btnNuovoTicket = (Button) rootView.findViewById(R.id.btnNuovoTicket);
	        btnNuovoTicket.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), NuovoTicketActivity.class);
	                startActivity(intent);
	            }
	        });
	        
	        btnContratto = (Button) rootView.findViewById(R.id.btnContratto);
	        btnContratto.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), ContrattoActivity.class);
	                startActivity(intent);
	            }
	        });
	        
	        btnTicket = (Button) rootView.findViewById(R.id.btnScegliTicket);
	        btnTicket.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), TicketsActivity.class);
	                startActivity(intent);
	            }
	        });
			
	        btnRapporto = (Button) rootView.findViewById(R.id.btnRapportiIntervento);
	        btnRapporto.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), RapportoInterventoActivity.class);
	                startActivity(intent);
	            }
	        });
	        
	        btnNuovoRapporto = (Button) rootView.findViewById(R.id.btnNuovoRapporto);
	        btnNuovoRapporto.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), NuovoRapportoActivity.class);
	                startActivity(intent);
	            }
	        });
	        
	        btnCalendario = (Button) rootView.findViewById(R.id.btnCalendario);
	        btnCalendario.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	Intent intent = new Intent(rootView.getContext(), CalendarioActivity.class);
	                startActivity(intent);
	            }
	        });
	        
	        if (GatSession.getInstance().getUtenteLoggato() instanceof Cliente)
	        {
	        	btnNuovoTicket.setVisibility(View.VISIBLE);
	        	btnContratto.setVisibility(View.VISIBLE);
	        	btnTicket.setVisibility(View.VISIBLE);
	        	btnRapporto.setVisibility(View.VISIBLE);
	        	btnNuovoRapporto.setVisibility(View.GONE);
	        	btnCalendario.setVisibility(View.GONE);
	        }
	        if (GatSession.getInstance().getUtenteLoggato() instanceof Operatore)
	        {
	        	btnNuovoTicket.setVisibility(View.GONE);
	        	btnContratto.setVisibility(View.GONE);
	        	btnTicket.setVisibility(View.VISIBLE);
	        	btnRapporto.setVisibility(View.VISIBLE);
	        	btnNuovoRapporto.setVisibility(View.VISIBLE);
	        	btnCalendario.setVisibility(View.VISIBLE);
	        }
	        
			return rootView;
		}
	}

}
